from typing import TypedDict
import uuid

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.constants import START
from langgraph.graph import StateGraph
from langgraph.types import interrupt, Command

class State(TypedDict):
    messsage: str
    age: str

# Human node进行中断
def human_node(state: State):
    value = interrupt( 
        {
            "messsage": state["messsage"] 
        }
    )
    return {
        "age": value 
    }


# Build the graph
graph_builder = StateGraph(State)
graph_builder.add_node("human_node", human_node)
graph_builder.add_edge(START, "human_node")

checkpointer = InMemorySaver() 

graph = graph_builder.compile(checkpointer=checkpointer)

# Pass a thread ID to the graph to run it.
config = {"configurable": {"thread_id": uuid.uuid4()}}

# Run the graph until the interrupt is hit.
result = graph.invoke({"messsage": "我的年龄是多少？"}, config=config) 

print(result['__interrupt__']) 

#输出[Interrupt(value={'messsage': '我的年龄是多少？'}, resumable=True, ns=['human_node:1a8de8ea-e477-7583-30aa-263f8e02571f'])]

#使用Command原语来恢复
print(graph.invoke(Command(resume="25"), config=config)) 

# 输出：{'messsage': '我的年龄是多少？', 'age': '25'}
